using System;
using System.Collections.Generic;
using System.Linq;
using BCWAccountVip;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine;

namespace BCW
{
	public class GpgWrapper
	{
		private const string _productsForPushKey = "ProductsForPush";

		private const string _productsFileName = "BCW_PRODUCTS";

		private const string _starsFileName = "BCW_STARS";

		private const string _versionAndMoneyFileName = "BCW_VERSION_MONEY";

		private const string _accountVipFileName = "BCW_ACCOUNT_VIP";

		private const string _moneyCheeter = "BCW_MONEY_CHEETER";

		private static GpgWrapper _instance;

		private PlayGamesPlatform _playGamesPlatformInstance;

		public static GpgWrapper instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new GpgWrapper();
				}
				return _instance;
			}
		}

		public PlayGamesPlatform PlayGamesPlatform
		{
			get
			{
				return _playGamesPlatformInstance;
			}
		}

		public ISavedGameClient SavedGame
		{
			get
			{
				try
				{
					return PlayGamesPlatform.SavedGame;
				}
				catch (NullReferenceException)
				{
					return null;
				}
			}
		}

		public event EventHandler SignedOut;

		private GpgWrapper()
		{
		}

		public void Init()
		{
			InitializePlugin();
			TryAuthenticate(null);
		}

		public void Tst()
		{
		}

		public void InitializePlugin()
		{
			BCWDebug.LogError("Initialize");
			try
			{
				PlayGamesClientConfiguration configuration = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
				PlayGamesPlatform.InitializeInstance(configuration);
				PlayGamesPlatform.DebugLogEnabled = BCWDebug.isDebugBuild;
				PlayGamesPlatform.Activate();
				_playGamesPlatformInstance = PlayGamesPlatform.Instance;
			}
			catch (Exception exception)
			{
				Debug.LogException(exception);
			}
		}

		public void TryAuthenticate(Action<bool> callback)
		{
			instance.Authenticate(delegate(bool x)
			{
				if (x)
				{
					BCWDebug.LogError("Success silent Authenticate", "GPG");
					if (callback != null)
					{
						callback(x);
					}
				}
				else
				{
					BCWDebug.LogError("Failed silent Authenticate", "GPG");
					instance.Authenticate(delegate(bool y)
					{
						if (y)
						{
							BCWDebug.LogError("Success normal Authenticate", "GPG");
						}
						else
						{
							BCWDebug.LogError("Failed normal Authenticate", "GPG");
						}
						if (callback != null)
						{
							callback(y);
						}
					}, false);
				}
			}, true);
		}

		public void Authenticate(Action<bool> callback, bool silent)
		{
			if (callback == null)
			{
				throw new ArgumentNullException("callback");
			}
			PlayGamesPlatform.Authenticate(callback, silent);
		}

		public void IncrementAchievement(string achievementId, int steps, Action<bool> callback)
		{
			if (achievementId == null)
			{
				throw new ArgumentNullException("achievementId");
			}
			if (callback == null)
			{
				throw new ArgumentNullException("callback");
			}
			PlayGamesPlatform.IncrementAchievement(achievementId, steps, callback);
		}

		public bool IsAuthenticated()
		{
			return PlayGamesPlatform.IsAuthenticated();
		}

		public void SignOut()
		{
			PlayGamesPlatform.SignOut();
			EventHandler signedOut = this.SignedOut;
			if (signedOut != null)
			{
				signedOut(this, EventArgs.Empty);
			}
		}

		public void LoadProducts(Action<string> completeHandler)
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_PRODUCTS", ProductsMerger, completeHandler);
			googlePullHelper.Run();
		}

		public void SaveProducts()
		{
			string data = BCWStorage.Get<string>("ProductsForPush");
			if (!string.IsNullOrEmpty(data))
			{
				GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_PRODUCTS", ProductsMerger, delegate(string result)
				{
					GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_PRODUCTS", ProductsMerger, ProductsMerger(result, data), CommitCompleteHandler);
					googlePushHelper.Run();
				});
				googlePullHelper.Run();
			}
			SaveVersionAndMoney();
		}

		public void DeleteProducts()
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_PRODUCTS", ProductsMerger, delegate(string result)
			{
				GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_PRODUCTS", ProductsMerger, ProductsMerger(result, string.Empty), CommitCompleteHandler);
				googlePushHelper.Run();
			});
			googlePullHelper.Run();
		}

		public void AddForFutureSave(string id)
		{
			string text = BCWStorage.Get<string>("ProductsForPush");
			text = text + id + ',';
			BCWStorage.Set("ProductsForPush", text);
		}

		private void CommitCompleteHandler(bool success)
		{
			if (success)
			{
				BCWStorage.Set("ProductsForPush", string.Empty, true);
			}
		}

		public void SaveStars()
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_STARS", StarsMerger, delegate(string result)
			{
				GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_STARS", StarsMerger, StarsMerger(result, CupsManager.Cups.ToString()), delegate(bool x)
				{
					BCWDebug.Log("Stars saved: " + x, "GPG");
				});
				googlePushHelper.Run();
			});
			googlePullHelper.Run();
		}

		public void LoadStars()
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_STARS", StarsMerger, delegate(string x)
			{
				int result = 0;
				if (int.TryParse(x, out result))
				{
					if (result > CupsManager.Cups)
					{
						CupsManager.RestoreCups(result);
						BCWDebug.Log("Stars restored: " + x, "GPG");
					}
					else
					{
						BCWDebug.Log("Stars not restored: " + x, "GPG");
					}
				}
				else
				{
					BCWDebug.Log("Cant parse stars data: " + x, "GPG");
				}
			});
			googlePullHelper.Run();
		}

		public void SaveAccountVip()
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_ACCOUNT_VIP", AccountVipMerger, delegate(string result)
			{
				GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_ACCOUNT_VIP", AccountVipMerger, AccountVipMerger(result, ManagerVip.GetInstance().GetExpireVipServer().ToString()), delegate(bool x)
				{
					BCWDebug.Log("Account vip saved: " + x, "GPG");
				});
				googlePushHelper.Run();
			});
			googlePullHelper.Run();
		}

		public void LoadAccountVip()
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_ACCOUNT_VIP", AccountVipMerger, delegate(string x)
			{
				int result = 0;
				if (int.TryParse(x, out result))
				{
					ManagerVip.GetInstance().RestoreExpire(result);
				}
				else
				{
					BCWDebug.Log("Cant parse account vip data: " + x, "GPG");
				}
			});
			googlePullHelper.Run();
		}

		public void SaveVersionAndMoney()
		{
			string text = settings.verConnectPhoton.ToCharArray().Aggregate(string.Empty, (string str, char ch) => (!char.IsDigit(ch)) ? str : (str + ch));
			string text2 = settings.CathedExeptions.ToString();
			string text3 = settings.CathedWarnings.ToString();
			string data = "version_cash_gold";
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_VERSION_MONEY", VersionAndMoneyMerger, delegate(string result)
			{
				GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_VERSION_MONEY", VersionAndMoneyMerger, VersionAndMoneyMerger(result, data), delegate(bool x)
				{
					BCWDebug.Log("Version and money saved: " + x, "GPG");
				});
				googlePushHelper.Run();
			});
			googlePullHelper.Run();
		}

		public void SaveMoneyCheeter(int isCheeter)
		{
			GooglePullHelper googlePullHelper = new GooglePullHelper("BCW_MONEY_CHEETER", MoneyCheeterMerger, delegate(string result)
			{
				GooglePushHelper googlePushHelper = new GooglePushHelper("BCW_MONEY_CHEETER", MoneyCheeterMerger, MoneyCheeterMerger(result, isCheeter.ToString()), delegate(bool x)
				{
					BCWDebug.Log("money cheeter saved: " + x, "GPG");
				});
				googlePushHelper.Run();
			});
			googlePullHelper.Run();
		}

		private static string ProductsMerger(string original, string unmerged)
		{
			HashSet<string> hashSet = new HashSet<string>();
			hashSet.UnionWith(new List<string>(original.Split(',')));
			hashSet.UnionWith(new List<string>(unmerged.Split(',')));
			return string.Join(",", hashSet.ToArray());
		}

		private static string StarsMerger(string original, string unmerged)
		{
			return MergaMaxValue(original, unmerged);
		}

		private static string AccountVipMerger(string original, string unmerged)
		{
			return MergaMaxValue(original, unmerged);
		}

		private static string MoneyCheeterMerger(string original, string unmerged)
		{
			return MergaMaxValue(original, unmerged);
		}

		private static string MergaMaxValue(string original, string unmerged)
		{
			int result = 0;
			int result2 = 0;
			bool flag = int.TryParse(original, out result);
			bool flag2 = int.TryParse(unmerged, out result2);
			if (flag || flag2)
			{
				if (!flag)
				{
					return unmerged;
				}
				if (!flag2)
				{
					return original;
				}
				return (result <= result2) ? unmerged : original;
			}
			return string.Empty;
		}

		private static string VersionAndMoneyMerger(string original, string unmerged)
		{
			int[] array = ParseVersionAndMoney(original);
			int[] unmergedData = ParseVersionAndMoney(unmerged);
			if (array == null && unmergedData == null)
			{
				return string.Empty;
			}
			if (array == null)
			{
				return string.Join("_", unmergedData.Select((int x) => x.ToString()).ToArray());
			}
			if (unmergedData == null)
			{
				return string.Join("_", array.Select((int x) => x.ToString()).ToArray());
			}
			string[] value = array.Select((int elem, int idx) => Mathf.Max(elem, unmergedData[idx]).ToString()).ToArray();
			return string.Join("_", value);
		}

		private static int[] ParseVersionAndMoney(string str)
		{
			string[] array = str.Split('_');
			if (array.Length != 3)
			{
				return null;
			}
			bool flag = true;
			int result;
			flag &= int.TryParse(array[0], out result);
			int result2;
			flag &= int.TryParse(array[1], out result2);
			int result3;
			if (!(flag & int.TryParse(array[2], out result3)))
			{
				return null;
			}
			return new int[3] { result, result2, result3 };
		}
	}
}
